<template>
  <div>
    <el-button type="primary" @click="addPortDialog = true">购买数据卷</el-button>
    <el-table class="table_el" :data="volumes" style="width: auto">
      <el-table-column label="ID" prop="id" />
      <el-table-column label="空间大小(GB)" prop="size" />
      <el-table-column label="创建时间" prop="created_at" />
      <el-table-column label="状态">
        <template #default="scope">
          <el-tag :type="volumeStatusColor(scope.row.state)">{{
            volumeStatus(scope.row.state)
            }}</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog v-model="addPortDialog" title="购买数据卷" width="600" draggable overflow>
      <div style="display: flex; flex-wrap: wrap">
        <el-form ref="formRef" :model="paylist" label-width="150px">
          <el-form-item label="购买大小(GB)" prop="size" :rules="[
            {
              required: true,
              message: '请输入购买大小(GB)',
              trigger: 'blur'
            }
          ]">
            <el-input v-model="paylist.size" type="number" :min="1" placeholder="请输入购买大小(GB)" />
          </el-form-item>
          <el-form-item label="支付方式">
            <el-checkbox label="优先使用余额支付" checked disabled />
            <el-radio v-model="paylist.pay_type" :label="0" size="large">支付宝</el-radio>
            <el-radio v-model="paylist.pay_type" :label="1" size="large">微信</el-radio>
          </el-form-item>
          <el-form-item v-if="pay_src != ''" label="扫码支付">
            <el-image style="width: 100px; height: 100px" :src="pay_src" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="pay_money">
          <el-statistic title="总计结算金额：" :value="payprice + ' 元'" suffix="">
            <template #suffix>
              <el-icon style="vertical-align: -20px">
                <ChatLineRound />
              </el-icon>
            </template>
          </el-statistic>
        </div>
        <div class="dialog-footer">
          <el-button @click="addPortDialog = false">关闭</el-button>
          <el-button type="primary" @click="topay">提交订单</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog v-model="pay_dialog" title="支付" style="width: 90%; height: 1000px">
      <iframe :srcdoc="pay_url" frameborder="0" style="width: 100%; height: 800px" />
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive, defineProps, watch, defineEmits } from "vue";
import { ElMessage } from "element-plus";
import { volumeStatusColor, volumeStatus } from "@/utils/hide";
import {
  getUserSnapshotList,
  createUserSnapshot,
  deleteSnapshot,
  recoverUserSnapshot,
  getVolumePrice,
  submitVolumeOrder
} from "@/api/virtual";
import { pay_request } from "@/api/user";
const props = defineProps(["ID", "volumes"]);
const emit = defineEmits(["GetLIst"]);
const formRef = ref(null); // 获取表单引用
const addPortDialog = ref(false);
const pay_dialog = ref(false);
const pay_url = ref("");
const pay_src = ref("");

const paylist = reactive({
  pay_type: "3",
  size: "",
  instance_id: props.ID
});

const payprice = ref("0");

// 获取价格
const purchaseVm = async () => {
  if (!paylist.size) return;
  await getVolumePrice({
    size: paylist.size,
    instance_id: paylist.instance_id
  }).then(res => {
    console.log(res);
    payprice.value = res.data.data.price;
  });
};

watch([() => paylist.size], () => {
  purchaseVm();
});

// 提交订单
const payloading = ref(false);

const topay = async () => {
  formRef.value.validate(async valid => {
    if (valid) {
      payloading.value = true;
      try {
        const res = await submitVolumeOrder(paylist);
        console.log(res);
        if (res.data.code === 200) {
          const payRes = await pay_request({
            pay_type: res.data.data.pay_type,
            trade_id: res.data.data.trade_id
          });
          console.log("支付信息", payRes);
          if (payRes.data.data.type === "html") {
            pay_url.value = payRes.data.data.html;
            pay_dialog.value = true;
          } else if (payRes.data.data.type === "img") {
            pay_src.value = payRes.data.data.img;
          } else {
            payloading.value = false;
            ElMessage.success("支付成功");
            paylist.size = "0";
            emit("GetLIst");
            addPortDialog.value = false;
          }
        }
      } catch (error) {
        console.error(error);
        payloading.value = false;
        ElMessage.error("提交订单失败");
      }
    }
  });
};
onMounted(() => { });
</script>

<style scoped></style>
